package com.ps.actions.setting;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.ps.actions.BaseAction;
import com.ps.entity.Result;
import com.ps.entity.Status;
import com.ps.entity.person.Manager;
import com.ps.global.Globals;
import com.ps.pojo.person.Employee;
import com.ps.service.person.EmployeeService;
import com.ps.service.person.imp.EmployeeServiceImp;
import com.ps.service.setting.AreaService;
import com.ps.service.setting.RoleService;
import com.ps.service.setting.imp.RoleServiceImp;
import com.ps.util.Kit;
import com.ps.util.PaginationSupport;
import com.ps.util.R;
import com.sun.org.apache.commons.beanutils.BeanUtils;

public class ManagerAction extends BaseAction {

	@Autowired
	private EmployeeService employeeService; 

	@Autowired
	private RoleService roleService;
	
	private String paginationJson;
	
	private String managerJson;
	
	private String password;
	
	private Integer employeeId;
	
	private Manager manager;
	
    private Integer  startIndex = 0;
	
	@SuppressWarnings("unchecked")
	public void findManagerByPage() {
		PaginationSupport paginationSupport
			= Kit.getFromJson(paginationJson, PaginationSupport.class);
		Result result = employeeService.findManagerByPage(paginationSupport.getPageSize(), paginationSupport.getStartIndex());
		System.out.println(Kit.toJson(result));
		outputJson(Kit.toJson(result));
	}
	
	public void addManager() {
		Manager manager = Kit.getFromJson(getManagerJson(), Manager.class);
		Result result = null;
		if (manager == null || StringUtils.isBlank(password)) {
			result = Kit.createResult();
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("addfail"));
		} else {
			manager.setInputerId(this.getSessionUserId());
			result = employeeService.addManager(manager, password);
		}
		
		outputJson(Kit.toJson(result));
	}
	
	public void updateManager() {
		Manager manager = Kit.getFromJson(getManagerJson(), Manager.class);
	    Result result = null;
		if (manager == null || StringUtils.isBlank(password)) {
			result = Kit.createResult();
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("addfail"));
		} else {
			
			manager.setInputerId(this.getSessionUserId());
			result = employeeService.updateManager(manager, password);
		}
		
		outputJson(Kit.toJson(result));
	}	
	
	

	public String jumpToUpdateManager() {
		System.out.println("employeeId = " + this.getEmployeeId() + " startIndex = " + startIndex);
		Employee employee = 
			(Employee) employeeService.findById(employeeId).getDatas();
		Manager manager = new Manager();
		try {
			BeanUtils.copyProperties(manager, employee);
			password = employee.getPassword();
			setManager(manager);
		} catch (Exception e) {
            return ERROR;
		}


		return "updateManagerPage";
	}
	
	public String jumpToAdd() {
		return "addManagerPage";
	}
	
	
	public String jumpToMainManager() {
		System.out.println(" startIndex = " + startIndex);



		return "main";
	}

	
	
	/**
	 * 查找管理员角色
	 */
	public void findManagerRole() {

		outputJson(Kit.toJson(roleService.findManagerRole()));
	}
	


	public String getPaginationJson() {
		return paginationJson;
	}

	public void setPaginationJson(String paginationJson) {
		this.paginationJson = paginationJson;
	}

	public void setManagerJson(String managerJson) {
		this.managerJson = managerJson;
	}

	public String getManagerJson() {
		return managerJson;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public Integer getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(Integer employeeId) {
		this.employeeId = employeeId;
	}

	public void setManager(Manager manager) {
		this.manager = manager;
	}

	public Manager getManager() {
		return manager;
	}

	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}

	public Integer getStartIndex() {
		return startIndex;
	}


	
}
